define(['jquery','template','Iscroll'], function($,template,IScroll) {
	var box = $("#portal-side .sidebar-box");
	var panel = $("#portal-panel");

	var navData = {}; //导航json的缓存
	var navControl = {
		/*初始化*/
		init:function(){
			initScroll();
			/*提供折叠功能*/
			box.on("click",".sidebar-link.hasChild",function(event){
				var t = $(this);
				var p = t.parent(".sidebar");
				if($("#portal-panel").hasClass('portal-panel-miniSize')){
					flushScroll();
					/*p.toggleClass("open").find(".sidebar-children").toggle();
					p.siblings(".sidebar").removeClass('open').find(".sidebar-children").hide();*/
				}else{
					p.toggleClass("open").find(".sidebar-children").slideToggle("fast",function(){
						flushScroll();
					});
					p.siblings(".sidebar").removeClass('open').find(".sidebar-children").slideUp("fast");
					
				}
				
			});
			

			//页面大小切换时,重置需要滚动的菜单
			$(document).on("click","#portal-toggle",function(){
				flushScroll();
			});
			
			//监听一级导航事件


			//一级导航
			$(document).on("click","#headerNav_index",function(){
				$("#portal-side .sidebar-box").html("");
				$("#portal-panel").addClass('portal-panel-miniSize');
			});
			/*绑定导航事件*/
			$(document).on("click","#headerNav_lv1 a.hasChild",function(){
				var t = $(this);
				$("#headerNav_lv1 li").removeClass("active");
				t.parent('li').addClass('active');
				//点击首页时
				var idx = t.data('index');
				var data2 = {data:navData.data[idx].nav};

				var html = template('_nav', data2);
				$("#portal-side .sidebar-box").html(html);

				
				openPanel();
				setTimeout(function(){
					$(".sidebar-link.hasChild").eq(0).parents('.sidebar').addClass('open').find(".sidebar-children").slideToggle("fast",function(){
						flushScroll();
					});;
				},200);
				

			});
		},
		/*加载菜单*/
		loadNav:function(data){
			navData = data; //缓存菜单json
			var html = template('_nav_lv1', data);
			
			$('#headerNav_index').after(html);
			resetNav1();
			$(window).on('resize',resetNav1);
		},
		/*加载子菜单*/
		loadChildrenNav:function(data){

		}
	}
	function openPanel(){
		$("#portal-detail").stop().animate({
			'left': '220px',
			
		},'fast');
		$("#portal-side").stop().animate({
			'width': '220px',
			
		},'fast', function() {
			panel.removeClass('portal-panel-miniSize');
			$("#portal-side .sidebar.open .sidebar-children").show();
		});
	}
	function resetNav1(){
		var logo = $("#portal-header .navbar-brand").outerWidth();
		var search = $("#headerNav_search").outerWidth();
		var nav1 = $("#headerNav_lv1");
		var navRight = $("#headerNav_right").outerWidth();;
		var more = $("#headerNavMore");
		var _window = $('body').width();
		var maxWidth = _window - logo - search - navRight -120 -30;
		/*console.log('maxWidth:'+maxWidth);
		console.log('_window:'+_window);
		console.log('logo:'+logo);
		console.log('search:'+search);
		console.log('navRight:'+navRight);*/
		nav1.css({
			opacity:0,
			left:logo,
			position:'absolute'
		});
		more.find('li').insertBefore(more)
		var li = nav1.find("li").not(".dropdown");
		var maxNum = li.length;
		var total = 0;
		li.each(function(index, el) {
			 var w  = $(this).outerWidth();
			 total += w;
			 var flag = total>maxWidth?true:false;
			 if(flag){
			 	console.log(index);
			 	maxNum = index-1 <1?0:index-1;
			 	return false;
			 } 
		});

		maxNum = maxNum>4?4:maxNum;
		
		var target = $("#headerNav_lv1 li:not('.dropdown'):gt("+maxNum+")");
		
		target.appendTo("#headerNavMore ul");
		
		var moreli = more.find('li').length;
		if (moreli>0) {
			more.show();
			moreli>=8?more.addClass('portal-navlist-col4'):more.removeClass('portal-navlist-col4');
		}else{
			more.hide();
		};
		
		if(_window>1000){
			$("#headerNav_search .form-control").css("width","auto");
		}else{
			$("#headerNav_search .form-control").css("width","100px");

		}

		nav1.css({
			opacity:1,
			left:'auto',
			position:'relative'
		})

	}

	var _scrollObj = {};

	function initScroll () {
		var defaultOption = { mouseWheel: true ,vScrollbar: false, scrollbars: false,fadeScrollbars:false};
		var target = $(".scroll-wrapper");
		target.each(function(index, el) {
			var id = $(this).attr("id");
			if(!id){
				id = 'custom-scorll'+index;
				$(this).attr('id',id);
			}
			var myScroll = new IScroll('#'+id, defaultOption);
			_scrollObj[id] = myScroll;
		});
	}

	function flushScroll(){
		var target = $(".scroll-wrapper");
		target.each(function(index, el) {
			var id = $(this).attr("id");
			
			
			_scrollObj[id].refresh();
		});
	}

	return {
		navControl : navControl
	}
});